<?php

namespace app\index\model\tan;

use think\Model;
use think\Db;
class Product extends Model
{
    //test
    public function x(){
//        $x=$this;
//        $this->transaction(function()use ( $x ){
//            $x
////                    $x
//                ->where(["id"=>1])
//                ->update([
//                    "price"=>111.65
//                ]);
////                sleep(10);
//                $x->rollback();
//        });
////        $this->lock(true);
//        Db::startTrans();
//        $d=Db::table('product') ->where(["id"=>1])
////                ->lock( "lock in share mode")
////                ->lock("lock in share mode")
//                ->lock(true)
//                ->select();
////         print_r($d);
//         sleep(10);
//         print_r($d);
////        Db::rollback();
//        Db::commit();
    }


    public function statistics($userid){
        $statistics['Product_count']= $this->where([
            "business_id"=>$userid,
            "is_shelf"=>3
        ])->count();#统计该用户目前上架的产品
        $statistics["product_browse"]=   $this->alias("p")
            ->join("product_other o","p.id=o.product_id")
            ->where(["p.business_id"=>$userid,"p.is_shelf"=>3]) 
            ->field(["sum(o.shares) as shares","sum(o.followed) as followed","sum(o.views) as views"])
            ->cache() //-------
            ->find()
            ->toArray();
        return $statistics;
    }
}
